package com.cc.screen;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class VideoUtils {

    public static void splitVideo(String inputPath, String outputPath, int startTime, int duration) {
        try {
            ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", inputPath, "-ss", "" + startTime, "-t", "" + duration, outputPath);
            pb.inheritIO();
            Process process = pb.start();
            process.waitFor();
            System.out.println("视频分割成功！");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void mergeVideos(String inputPath1, String inputPath2, String outputPath) {
        try {
            ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", "concat:" + inputPath1 + "|" + inputPath2, "-c", "copy", outputPath);
            pb.inheritIO();
            Process process = pb.start();
            process.waitFor();
            System.out.println("视频合并成功！");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        splitVideo("E:\\video\\20241125_141851_3.mp4", "E:\\video\\output.mp4", 1, 1);
//        //mergeVideos("input1.mp4", "input2.mp4", "output.mp4");
//    }


    public static void main(String[] args) {
        String videoFile = "E:\\video\\20241125_141851_3.mp4"; // 视频文件路径
        String subtitleFile = "E:\\video\\1734509672378.srt"; // 字幕文件路径
        String outputFile = "E:\\video\\testzm.mp4"; // 输出视频文件路径

        // 构建ffmpeg命令
        String[] command = new String[]{
                "ffmpeg",
                "-i", videoFile,
                "-i", subtitleFile,
                "-c:v", "copy",
                "-c:a", "copy",
                "-s", "640x480", // 可以指定视频的分辨率
                "-b:a", "64k", // 可以指定音频的比特率
                "-shortest", // 确保字幕不会被剪裁
                outputFile
        };

        try {
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 打印ffmpeg的输出信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            process.waitFor(); // 等待ffmpeg命令执行完成

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}